package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 99037
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    MemberService memberService;

    @Reference
    OrderService orderService;

    @RequestMapping("/getMemberAgeReport")
    public Result getMemberAgeReport(){
      try {
          List<Map<String,Object>> list=memberService.findMemberAge();
          List<String> age=new ArrayList<>();
          List<Object> value=new ArrayList<>();

          for (Map<String, Object> map : list) {

              String age1 = (String) map.get("age");
              age.add(age1);

              Object value1 = map.get("value");

              value.add(value1);
          }
          Map map=new HashMap();
          map.put("age",age);
          map.put("value",value);


          return new Result(true, MessageConstant.GET_MEMBER_AGE_SUCCESS,map);
      }catch (Exception e){
          e.printStackTrace();
          return new Result(false,MessageConstant.GET_MEMBER_AGE_FAIL);
      }
    }

    @RequestMapping("/memberDataCount")
    public Result memberDataCount(String[] value){
        Map<String,Object> map=new HashMap<>();

        try{
            System.out.println(value);

            //开始日期
            String dBegin=value[0];
            //结束日期
            String dEnd=value[1];


            System.out.println(dBegin);

            //日期工具类准备
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            //设置开始时间
            Calendar calBegin = Calendar.getInstance();

            calBegin.setTime(format.parse(dBegin));

            //设置结束时间
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(format.parse(dEnd));

            //装返回的日期集合容器
            List<String> Datelist = new ArrayList<String>();
            //将第一个月添加里面去
            Datelist.add(format.format(calBegin.getTime()));
            // 每次循环给calBegin日期加一天，直到calBegin.getTime()时间等于dEnd
            while (format.parse(dEnd).after(calBegin.getTime()))  {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                Datelist.add(format.format(calBegin.getTime()));
            }


            //每日预约数
            List<Integer> orders=  orderService.findOrderCountByDate(Datelist);
            //每日到诊数
            List<Integer> arrives=  orderService.findVisitsCountByDate(Datelist);

            System.out.println(Datelist);
            map.put("Datelist",Datelist);
            map.put("orders",orders);
            map.put("arrives",arrives);
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }



    }

    @RequestMapping("/incomeCount")
    public Result incomeCount(String[] value){
        Map<String,Object> map=new HashMap<>();

        try{
            System.out.println(value);

            //开始日期
            String dBegin=value[0];
            //结束日期
            String dEnd=value[1];


            System.out.println(dBegin);

           List<Map<String,Object>> setmealCount=memberService.findSetmealIncomeByDate(dBegin,dEnd);

           List setmealNames=new ArrayList();
            for (Map<String, Object> map1 : setmealCount) {
                String setmealName = (String) map1.get("name");
                setmealNames.add(setmealName);
            }
            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);

            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }



    }
}
